package com.xxbing.controller;


import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageInfo;
import com.xxbing.dto.EdocEntryDto;
import com.xxbing.model.EdocCategory;
import com.xxbing.model.EdocEntry;
import com.xxbing.service.EdocCategoryService;
import com.xxbing.service.EdocEntryService;

@Controller
@RequestMapping("/edocEntry")
public class EdocEntryController {
	@Autowired
	private EdocEntryService edocEntryService;
	
	@Autowired
	private EdocCategoryService edocCategoryService;
	
	
	@RequestMapping("/selectByInfo")
	public String selectByPage(EdocEntryDto dto,Model model) {
		PageInfo pageInfo = edocEntryService.selectByPages(dto);
		model.addAttribute("pageInfo", pageInfo);
		
		List<EdocEntry> edocEntries = pageInfo.getList();
		model.addAttribute("edocEntries", edocEntries);	
		
		List<EdocCategory> edocCategory = edocCategoryService.selectAll();
		model.addAttribute("edocCategory", edocCategory);
		if(dto.getCategoryId() > 0 ) {
			model.addAttribute("edocCategory2", edocCategoryService.selectEdocCategoryByid(dto.getCategoryId()));
			model.addAttribute("select_val", 1); //返回的是有查询条件
		}else {
			model.addAttribute("select_val", -1);  //返回的是没有查询条件，即查询全部
		}
		return "main";
	}
	
	//根据条件查询
	@RequestMapping("/selectByCategoryid")
	public String selectByCategoryid(Model model,@RequestParam int categoryId) {
		PageInfo pageInfo = edocEntryService.selectByCategoryId(categoryId);
		model.addAttribute("pageInfo", pageInfo);
		
		List<EdocEntry> edocEntries = pageInfo.getList();
		model.addAttribute("edocEntries", edocEntries);	
		
		List<EdocCategory> edocCategory = edocCategoryService.selectAll();
		model.addAttribute("edocCategory", edocCategory);
		if(categoryId > 0 ) {
			model.addAttribute("edocCategory2", edocCategoryService.selectEdocCategoryByid(categoryId));
			model.addAttribute("select_val", 1); //返回的是有查询条件
		}else {
			model.addAttribute("select_val", -1);  //返回的是没有查询条件，即查询全部
		}
		return "main";
	}
	
	
	@RequestMapping("/toAddDoc")
	public String toAddDoc(Model model,@RequestParam int categoryId) {
		
		EdocCategory edocCategory2 =  edocCategoryService.selectEdocCategoryByid(categoryId);  //查询文档分类名称
		model.addAttribute("edocCategory2", edocCategory2);  
		return "addDoc";
	}
	
	
	@RequestMapping("/addDoc")
	public String addDoc(Model model,EdocEntry edoc) {
		
		PageInfo pageInfo = edocEntryService.addDoc(edoc);
		model.addAttribute("pageInfo", pageInfo);
		
		List<EdocEntry> edocEntries = pageInfo.getList();
		model.addAttribute("edocEntries", edocEntries);	
		
		List<EdocCategory> edocCategory = edocCategoryService.selectAll();
		model.addAttribute("edocCategory", edocCategory);
		if(edoc.getCategoryId() > 0 ) {
			model.addAttribute("edocCategory2", edocCategoryService.selectEdocCategoryByid(edoc.getCategoryId()));
			model.addAttribute("select_val", 1); //返回的是有查询条件
		}else {
			model.addAttribute("select_val", -1);  //返回的是没有查询条件，即查询全部
		}
		return "main";
	}
	
	
	@RequestMapping("/toUpdate/{id}")
	public String toUpdate(@PathVariable("id") int id,@RequestParam("pageNum") Integer pageNum,@RequestParam("selectVal") Integer selectVal, Model model) {
		EdocEntry edoc = edocEntryService.selectById(id);
		model.addAttribute("edoc", edoc);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("selectVal", selectVal);
		return "editDoc";
	}
	
	
	@RequestMapping("/update")
	public String update(EdocEntry edoc,@RequestParam("pageNum") Integer pageNum,@RequestParam("selectVal") Integer selectVal, Model model) {
		
		PageInfo pageInfo = edocEntryService.update(edoc,pageNum,selectVal);
		model.addAttribute("pageInfo", pageInfo);
		
		List<EdocEntry> edocEntries = pageInfo.getList();
		model.addAttribute("edocEntries", edocEntries);	
		
		List<EdocCategory> edocCategory = edocCategoryService.selectAll();
		model.addAttribute("edocCategory", edocCategory);
		
		if(selectVal == 1) {
			model.addAttribute("edocCategory2", edocCategoryService.selectEdocCategoryByid(edoc.getCategoryId()));
			model.addAttribute("select_val", 1); //返回的是有查询条件
		}else {
			model.addAttribute("select_val", -1);  //返回的是没有查询条件，即查询全部
		}
		
		return "main";
	}
	
	
	
	@RequestMapping("/deleteById/{id}")
	public String deleteById(@PathVariable("id") int id,@RequestParam("pageNum") Integer pageNum,
			@RequestParam("selectVal") Integer selectVal,Integer categoryId, Model model) {
		
		PageInfo pageInfo = edocEntryService.deleteById(id,pageNum,selectVal,categoryId);
		model.addAttribute("pageInfo", pageInfo);
		
		
		List<EdocEntry> edocEntries = pageInfo.getList();
		model.addAttribute("edocEntries", edocEntries);	
		
		List<EdocCategory> edocCategory = edocCategoryService.selectAll();
		model.addAttribute("edocCategory", edocCategory);
		
		
		if(selectVal == 1) {
			model.addAttribute("edocCategory2", edocCategoryService.selectEdocCategoryByid(categoryId));
			model.addAttribute("select_val", 1); //返回的是有查询条件
		}else {
			model.addAttribute("select_val", -1);  //返回的是没有查询条件，即查询全部
		}
		
		model.addAttribute("msg", "删除成功");
		
		return "main";
	}
	
	
	
	
	
	
	/*@RequestMapping("/selectByCategoryid")
	public String selectByCategoryid(@RequestParam ("categoryId") int categoryId,Model model) {
		List<EdocEntry> edocEntries = null;
		EdocCategory edocCategory2 = null;
		if(categoryId>0) {
			edocEntries = edocEntryService.selectByCategoryid(categoryId);
			
			int id = categoryId;
			edocCategory2 =  edocCategoryService.selectEdocCategoryByid(id);  //查询文档分类名称
		}else {
			edocEntries = edocEntryService.selectAll();
		}
		model.addAttribute("edocEntries", edocEntries);
		model.addAttribute("edocCategory", edocCategoryService.selectAll());
		model.addAttribute("edocCategory2", edocCategory2);  
		return "main";
	}
	
	
	@RequestMapping("/toAddDoc")
	public String toAddDoc(@RequestParam int categoryId,Model model) {
		int id = categoryId;
		EdocCategory edocCategory2 =  edocCategoryService.selectEdocCategoryByid(id);  //查询文档分类名称
		model.addAttribute("edocCategory2", edocCategory2);  
		return "addDoc";
	}
	
	
	 @InitBinder
	    public void initBinder(WebDataBinder binder, WebRequest request) {
	        //转换日期格式
	        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        //注册自定义的编辑器
	        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	        
	    }

	@RequestMapping("/addDoc")
	public String addDoc(@RequestParam int categoryId,@RequestParam String title,@RequestParam String summary,
			@RequestParam String uploadUser,@RequestParam Date createDate,Model model) {
		EdocEntry edoc = new EdocEntry();
		edoc.setCategoryId(categoryId);
		edoc.setCreateDate(createDate);
		edoc.setSummary(summary);
		edoc.setTitle(title);
		edoc.setUploadUser(uploadUser);
		List<EdocEntry> edocEntries = null;
		EdocCategory edocCategory2 = null;
		int id =  categoryId;
		int row = edocEntryService.insertEdocEntry(edoc);
		edocEntries= edocEntryService.selectByCategoryid(categoryId);
		edocCategory2=edocCategoryService.selectEdocCategoryByid(id);
	    model.addAttribute("edocEntries", edocEntries);
		model.addAttribute("edocCategory", edocCategoryService.selectAll());
		model.addAttribute("edocCategory2", edocCategory2); 
		return "main";
	}
	
	
	@RequestMapping("/toUpdate/{id}")
	public String toUpdate(@PathVariable("id") int id, Model model) {
//	public String toUpdate(@PathVariable("id") int id,@RequestParam("categoryId") int categoryId, Model model) {
		EdocEntry edoc = edocEntryService.selectById(id);
		System.out.println("title ="+edoc.getTitle());
		model.addAttribute("edoc", edoc);
//		model.addAttribute("categoryId", categoryId);
		return "editDoc";
	}
	
	@RequestMapping("/update")
	public String update(EdocEntry edoc,Model model) {
		int row = edocEntryService.updateDoc(edoc);
		int id = edoc.getId();
		EdocEntry e = edocEntryService.selectById(id);
		int categoryId = e.getCategoryId();
		List<EdocEntry> edocEntries = edocEntryService.selectByCategoryid(categoryId);
		model.addAttribute("edocEntries", edocEntries);
		List<EdocCategory> edocCategory = edocCategoryService.selectAll();
		model.addAttribute("edocCategory", edocCategory);
		EdocCategory edocCategory2 = edocCategoryService.selectEdocCategoryByid(categoryId);
		model.addAttribute("edocCategory2", edocCategory2); 
		return "main";
	}
	
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable int id,Model model,HttpServletRequest req) {
		int row = edocEntryService.deleteDoc(id);
		List<EdocEntry> edocEntry = edocEntryService.selectAll();
		model.addAttribute("edocEntries", edocEntry);
		List<EdocCategory> edocCategory = edocCategoryService.selectAll();
		model.addAttribute("edocCategory", edocCategory);
		HttpSession session =  req.getSession();
		session.setMaxInactiveInterval(1);
		session.setAttribute("msg", "删除成功");
//		model.addAttribute("msg", "删除成功");
		return "main";
	}*/
	
	
}
